package constant

import (
	"github.com/lab-online/internal/shared/code"
	"github.com/lab-online/pkg/errors"
	"github.com/lab-online/pkg/gen"
	"github.com/lab-online/pkg/resp"
)

const (
	codeCourseNotExist            = resp.CodeBadRequest + domainCode
	codeNoPermissionToCreateClass = codeCourseNotExist + 1
	codeClassNotFound             = codeCourseNotExist + 2
	codeNoPermissionToUpdateClass = codeCourseNotExist + 3
	codeUserIsNotStudent          = codeCourseNotExist + 4
	codeUserNotFound              = codeCourseNotExist + 5
	codeNoPermissionToRemoveClass = codeCourseNotExist + 6
	codeNoPermissionToSearchClass = codeCourseNotExist + 7
	codeTeacherNotExist           = codeCourseNotExist + 8
)

const (
	dbUnknownError  = code.DatabaseUnknownError
	dbClassNotFound = code.DatabaseClassNotFound
)

var (
	RESTErrClassNotFound             = errors.NewRESTError(400, codeClassNotFound, "未找到指定班级")
	RESTErrCourseNotExist            = errors.NewRESTError(400, codeCourseNotExist, "课程不存在")
	RESTErrTeacherNotExist           = errors.NewRESTError(400, codeTeacherNotExist, "教师不存在")
	RESTErrNoPermissionToCreateClass = errors.NewRESTError(403,
		codeNoPermissionToCreateClass, "无权限创建非自己任教的班级")
	RESTErrNoPermissionToUpdateClass = errors.NewRESTError(403,
		codeNoPermissionToUpdateClass, "无权限更新非自己任教的班级")
	RESTErrNoPermissionToRemoveClass = errors.NewRESTError(403,
		codeNoPermissionToRemoveClass, "无权限删除非自己任教的班级")
	RESTErrNoPermissionToSearchClass = errors.NewRESTError(403,
		codeNoPermissionToSearchClass, "无权限查询非自己任教或自己所在的班级")
	RESTErrUserIsNotStudent = errors.NewRESTError(400, codeUserIsNotStudent, "用户不是学生")
	RESTErrUserNotFound     = errors.NewRESTError(400, codeUserNotFound, "用户不存在")

	DBErrUnknown       = errors.NewDBError(dbUnknownError, "class 表未定义错误")
	DBErrClassNotFound = errors.NewDBError(dbClassNotFound, "未找到指定班级")
)

func ErrCodeMap() gen.CodeMap {
	return gen.ParseCodeMapFromErrors(gen.ErrorMap{
		"CreateClass": {
			RESTErrCourseNotExist,
			RESTErrTeacherNotExist,
			RESTErrNoPermissionToCreateClass,
			DBErrUnknown,
		},
		"SearchClass": {
			RESTErrNoPermissionToSearchClass,
			DBErrUnknown,
		},
		"RemoveClass": {
			RESTErrClassNotFound,
			RESTErrNoPermissionToRemoveClass,
			DBErrUnknown,
		},
		"UpdateClass": {
			RESTErrClassNotFound,
			RESTErrNoPermissionToUpdateClass,
			DBErrUnknown,
		},
		"GetClass": {
			RESTErrNoPermissionToSearchClass,
			RESTErrClassNotFound,
			RESTErrUserNotFound,
			DBErrUnknown,
		},
		"UpdateStudent": {
			RESTErrClassNotFound,
			RESTErrNoPermissionToUpdateClass,
			RESTErrUserIsNotStudent,
			RESTErrUserNotFound,
		},
		"GetClassStudents": {
			RESTErrNoPermissionToSearchClass,
			RESTErrClassNotFound,
			RESTErrUserNotFound,
			DBErrUnknown,
		},
	})
}
